import axios from 'axios'
import store from '../store/store'
// axios.defaults
const service = axios.create({
  baseURL: 'http://www.chamiw.com/index.php', //后台地址
  transformRequest: [function (data) {
    let ret = ''
    for (let it in data) {
      ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
    }
    return ret
  }],
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  timeout: 150000 //请求超时时间
})
service.interceptors.response.use(
  response => {
    //请求正常则返回
    return Promise.resolve(response)
  },
  error => {
    // 请求错误则向store commit这个状态变化
    const httpError= { 
      hasError: true,
      status: error.response.status,
      statusText: error.response.statusText
    }
    store.commit('ON_HTTP_ERROR', httpError)
    return Promise.reject(error)
  }
)
export default service